//
//  HomeViewController.swift
//  DouYuSwift
//
//  Created by dingjunchuxing on 2019/7/3.
//  Copyright © 2019 text. All rights reserved.
//

import UIKit

let kTitleViewH : CGFloat = 40

class HomeViewController: UIViewController {

    private lazy var pageTitleView : PageTitleView = { [weak self] in
        let titleFrame = CGRect(x: 0, y: kNavStatueH+kNavH, width: kScreenW, height: kTitleViewH)
        let titles = ["推荐","游戏","娱乐","趣玩"]
 
        let titleView = PageTitleView(frame: titleFrame, titles: titles)
        
        titleView.backgroundColor = UIColor.white
        titleView.delegate = self
        return titleView
    }()

    private lazy var pageContentView : PageContentView = { [weak self] in
        
        var childVcs = [UIViewController]()
        for index in 0..<4{
             var  vc = UIViewController()
            if index == 0{
                 vc = RecommendViewController()
            }
            vc.view.backgroundColor = UIColor(r: CGFloat(arc4random_uniform(255)) , g: CGFloat(arc4random_uniform(255)), b:  CGFloat(arc4random_uniform(255)))
            childVcs.append(vc)
        }
        
        let contentH = kScreenH - kNavStatueH - kNavH - kTitleViewH - kTabBarH
        let contentFrame = CGRect(x: 0, y: kNavStatueH + kNavH + kTitleViewH, width: kScreenW, height: contentH)
        
        let pageView = PageContentView(frame: contentFrame, viewContollers: childVcs, pVc: self)
        pageView.delegate = self
        return pageView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI();
    }

}

extension HomeViewController{
    
    private func setupUI() {
        
        setupNavigation();
        
        view.addSubview(pageTitleView)
        
        view.addSubview(pageContentView)
    }
    
    private func setupNavigation(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(NormalImage: "logo")
        
        let size = CGSize(width: 40, height: 40)
        let historyItem = UIBarButtonItem(NormalImage: "image_my_history", HightlyImage: "Image_my_history_click", size: size)
        let searchItem = UIBarButtonItem(NormalImage: "btn_search", HightlyImage: "btn_search_clicked", size: size)
        let qrcodeItem = UIBarButtonItem(NormalImage: "Image_scan", HightlyImage: "Image_scan_click", size: size)
        navigationItem.rightBarButtonItems = [historyItem,searchItem,qrcodeItem]
    }
}

extension HomeViewController : PageTitleViewDelegate{
    func pageTitleView(titleView: PageTitleView, selectIndex Index: Int) {
        
        pageContentView.changeContentView(slectIndex: Index)
    }
    
}

// MARK:- 遵守PageContentViewDelegate协议
extension HomeViewController : PageContentViewDelegate {
    func pageContentView(_ contentView: PageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        pageTitleView.setTitleWithProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
}
